package com.example.elmmerchants.Shop_Activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Collect_And_Have_Gifts_Page extends AppCompatActivity {

    TextView set_shop_name, how_much_is_the_discount, how_much_can_use_discount, set_coupon_can_use_how_many_day, how_many_day_is_effective_in_show_coupon;
    EditText set_how_much_is_the_discount, set_how_much_can_use_discount;
    RelativeLayout set_day;
    CheckBox is_read_and_accept;
    LinearLayout create_data;

    String email, shop_name;
    String[] all_min = new String[]{"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"};
    boolean is_finish_upload_data = false;
    AlertDialog dialog;

    private class Upload_Data_Thread extends Thread {
        @Override
        public void run() {
            is_finish_upload_data = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "insert into `elm`.`merchant_coupon_collect_and_get_give` (email, set_how_much_is_the_discount, set_how_much_can_use_discount, set_coupon_can_use_how_many_day) VALUES (?, ?, ?, ?)";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, email);
                    preparedStatement.setInt(2, Integer.parseInt(set_how_much_is_the_discount.getText().toString()));
                    preparedStatement.setInt(3, Integer.parseInt(set_how_much_can_use_discount.getText().toString()));
                    preparedStatement.setInt(4, Integer.parseInt(set_coupon_can_use_how_many_day.getText().toString()));
                    preparedStatement.executeUpdate();

                    is_finish_upload_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_finish_upload_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    Intent intent = new Intent(Collect_And_Have_Gifts_Page.this, Check_All_Activity_Page.class);
                    setResult(1, intent);
                    finish();
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect_and_have_gifts_page);

        email = getIntent().getStringExtra("email");
        shop_name = getIntent().getStringExtra("shop_name");

        set_shop_name = findViewById(R.id.set_shop_name);
        set_how_much_is_the_discount = findViewById(R.id.set_how_much_is_the_discount);
        how_much_is_the_discount = findViewById(R.id.how_much_is_the_discount);
        set_how_much_can_use_discount = findViewById(R.id.set_how_much_can_use_discount);
        how_much_can_use_discount = findViewById(R.id.how_much_can_use_discount);
        set_day = findViewById(R.id.set_day);
        set_coupon_can_use_how_many_day = findViewById(R.id.set_coupon_can_use_how_many_day);
        how_many_day_is_effective_in_show_coupon = findViewById(R.id.how_many_day_is_effective_in_show_coupon);
        is_read_and_accept = findViewById(R.id.is_read_and_accept);
        create_data = findViewById(R.id.create_data);

        set_shop_name.setText(shop_name);

        set_how_much_is_the_discount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                how_much_is_the_discount.setText(set_how_much_is_the_discount.getText().toString());
            }
        });

        set_how_much_can_use_discount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                how_much_can_use_discount.setText(set_how_much_can_use_discount.getText().toString());
            }
        });

        set_day.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Dialog dialog = new Dialog(view.getContext());
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.choose_can_use_how_many_day_in_coupon_page);

                TextView time_text_view = dialog.findViewById(R.id.time_text_view);
                time_text_view.setText("7");

                NumberPicker how_many_time = dialog.findViewById(R.id.how_many_day);
                how_many_time.setMaxValue(all_min.length - 1);
                how_many_time.setDisplayedValues(all_min);
                how_many_time.setMinValue(0);
                how_many_time.setValue(0);
                how_many_time.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                    @Override
                    public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                        time_text_view.setText(String.valueOf(numberPicker.getValue() + 7));
                    }
                });

                Button close_page = dialog.findViewById(R.id.close_page);
                close_page.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });

                Button save = dialog.findViewById(R.id.save);
                save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        set_coupon_can_use_how_many_day.setText(time_text_view.getText().toString());
                        how_many_day_is_effective_in_show_coupon.setText(time_text_view.getText().toString());
                        dialog.dismiss();
                    }
                });

                dialog.show();
                dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                dialog.getWindow().setGravity(Gravity.BOTTOM);
            }
        });

        create_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (set_how_much_is_the_discount.getText().toString().isEmpty() || set_how_much_can_use_discount.getText().toString().isEmpty()) {
                    Toast.makeText(Collect_And_Have_Gifts_Page.this, "请输入所有资料", Toast.LENGTH_SHORT).show();
                }
                else if (!is_read_and_accept.isChecked()) {
                    Toast.makeText(Collect_And_Have_Gifts_Page.this, "请确认已阅读并同意协议", Toast.LENGTH_SHORT).show();
                }
                else {
                    set_dialog();
                    Upload_Data_Thread uploadDataThread = new Upload_Data_Thread();
                    uploadDataThread.start();
                }
            }
        });
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater1 = LayoutInflater.from(this);
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }
}